/**
 * service_member js file;
 * company:junqitecch
 * author:xuzuiotao
 * 
 */

//test
//
// LOGOUT:32,
//        USER_DETAIL:33,
//        USER_CHANGE_PASSWORD:34,


/**
 * login js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */

var  consts  = require('../consts');
var member= require('../db/member');
var db= require('../db/dbconnection');

var oauth2= require('../network/oauth2');


async function processOfficialLogin(request,response,callback){
	
	console.log('processOfficialLogin');	
	var data =request.data;
	var phone = null;
	var password = null;
	var muuid = null;
	var code = null;
	var isMA = 0;
	var simple = 0;
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return;
	}

	var param=null;
	try {
	    param = JSON.parse(data);
	} catch (e) {
	}
	
	muuid = request.muuid;
	
	if(param===null || typeof(param.type)==='undefined' || typeof(param.authCode)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'type and authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(typeof(param.authCode)==='undefined' || param.authCode===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	code = param.authCode;
	
	if(param.type !==1 && param.type!==2){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}
	
	if(param.simple && param.simple ===1){
		simple = 1;
	}
	
	var type = param.type;
	var conn = await db.createDB();
	if(!conn){
		response.errorCode=consts.Error.ERROR_BIND_ERROR;
		response.message=consts.Message.ERROR_BIND_ERROR;
		if(callback){
			callback(response);
		}
		return false;
	}
	var obj = await member.fetchAppIDandOpenID1(conn,muuid,code,type, null);
	if(!obj){
		response.errorCode=consts.Error.ERROR_BIND_ERROR;
		response.message=consts.Message.ERROR_BIND_ERROR;
		if(callback){
			callback(response);
		}
		db.releaseDB(conn);
		return false;
	}
		
	console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));
	
	var entity = null;
	if(!obj.openID){
				
		db.releaseDB(conn);
		var appid = obj.appid;
		var secret = obj.appsecret;
		var err = null;
		if(type===1){
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try {
				obj.openID = await oauth2.loginWeixin(appid, secret, code,isMA,null);
			}catch(e){
				err = e;
			}
		}else if(type===2){
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			
			try{
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err = e;
			}
		}
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return false;
		}
		conn = await db.createDB();
		if(!conn){
			response.errorCode=consts.Error.ERROR_BIND_ERROR;
			response.message=consts.Message.ERROR_BIND_ERROR;
			if(callback){
				callback(response);
			}
			return false;
		}
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type,null);
		
	}

	var entity = await member.check1(conn,muuid,obj.openID, type,simple,  null);	
	console.log('entity::' + JSON.stringify(entity));
	db.releaseDB(conn);

	if(entity===null){
		response.errorCode=consts.Error.ERROR_BIND_ERROR;
		response.message=consts.Message.ERROR_BIND_ERROR;
		if(callback){
			callback(response);
		}
	}else{
		console.log('entity::' +3333);
		if(entity.member){
			entity.isMember = 1;
		}
		console.log('entity::' +4444);

		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(entity);

		if(callback){
			callback(response);
		}
	}
	
	return true;
	
}


async function processRegister(request,response,callback){
	
	/*
	 * `id`, `username`, `phone`, `code`, `create_time`, `end_time`, `price`, `flag`, `type`, `uid`
	 */
	var data =request.data;
	var phone = null;
	var password = null;
	var muuid = null;
	var code = null;
	var memberName=null;
	var birth = null;
	var gender = null;
	var isMA = 0;
	var level=1;

	console.log('processRegister::'+ data);

	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return;
	}

	var param=null;
	try {
	    param = JSON.parse(data);

	} catch (e) {
	}
	
	muuid = request.muuid;
	
	if(param===null || typeof(param.type)==='undefined' || typeof(param.authCode)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'type and authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.type !==1 && param.type!==2){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(typeof(param.phone)==='undefined' || typeof(param.password)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':phone password';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(typeof(param.authCode)==='undefined' || param.authCode===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	

	if(typeof(param.memberName)!=='undefined' && param.memberName!==null){
		memberName= param.memberName;
	}
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}

	phone = param.phone;
	password = param.password;
	var type = param.type;
	code = param.authCode;
	
	if(param.level===0){
		level=param.level;
	}
	
	if(phone ===null  || password===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':phone password';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	var conn = await db.createDB();
	
	if(!conn){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;
	}
		
	var obj = await member.fetchAppIDandOpenID1(conn,muuid,code,type, null);
	if(obj===null){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}

		db.releaseDB(conn);

		return false;
	}
	console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));
	if(!obj.openID){	
		db.releaseDB(conn);
		var appid = obj.appid;
		var secret = obj.appsecret;
		var err = null;
		if(type===1){
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try{
				obj.openID = await oauth2.loginWeixin(appid, secret, code,isMA,null);
			}catch(e){
				err = e;
			}
				
		}else{
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			try {
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err = e;
			}
		}
		
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return false;
		}
			
		conn = await db.createDB();
		
		if(!conn){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
			return false;
		}
		
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type,null);
	}
			
	var entity = await member.register1(conn,muuid,obj.openID, type,phone,password,memberName,birth,gender,level, null);
					
	db.releaseDB(conn);

	if(entity===null){
		response.errorCode=consts.Error.ERROR_MOBILE_USED;
		response.message=consts.Message.ERROR_REGISTER;
		if(callback){
			callback(response);
		}
	}else{
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(entity);

		if(callback){
			callback(response);
		}
	}
	return true;	
	
}



async function processBind(request,response,callback){
	
	var data =request.data;
	var phone = null;
	var password = null;
	var muuid = null;
	var code = null;
	console.log('processBind::'+ data+':'+request.muuid);
	var isMA = 0;


	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return;
	}

	var param=null;
	try {
	    param = JSON.parse(data);

	} catch (e) {
	}
	
	muuid = request.muuid;
	
	if(param===null || typeof(param.type)==='undefined' || typeof(param.authCode)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'type and authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.type !==1 && param.type!==2){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(typeof(param.phone)==='undefined' || typeof(param.password)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':phone password';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(typeof(param.authCode)==='undefined' || param.authCode===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	phone = param.phone;
	password = param.password;
	var type = param.type;
	code = param.authCode;
	if(phone ===null  || password===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':phone password';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}
	
	var conn = await db.createDB();
	if(!conn){
		response.errorCode=consts.Error.ERROR_BIND_ERROR;
		response.message=consts.Message.ERROR_BIND_ERROR;
		if(callback){
			callback(response);
		}
		return false;
	}
		
	var obj = await member.fetchAppIDandOpenID1(conn,muuid,code,type, null);
	if(obj===null){
		response.errorCode=consts.Error.ERROR_BIND_ERROR;
		response.message=consts.Message.ERROR_BIND_ERROR;
		if(callback){
			callback(response);
		}

		db.releaseDB(conn);

		return;
	}
			
	console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));
			
	if(!obj.openID){
		db.releaseDB(conn);
		var appid = obj.appid;
		var secret = obj.appsecret;
		var err = null;
		if(type===1){
			
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try{
				obj.openID = await oauth2.loginWeixin(appid, secret, code,isMA, null);
			}catch(e){
				err = e;
			}
			
		}else if(type===2){
			
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			try{
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err =e;
			}
		}	
			
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return false;
		
		}
		conn = await db.createDB();
		if(!conn){
			response.errorCode=consts.Error.ERROR_BIND_ERROR;
			response.message=consts.Message.ERROR_BIND_ERROR;
			if(callback){
				callback(response);
			}
			return false;
		}
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type,null);
	
	}			
	var entity = await member.bind1(conn,muuid,obj.openID, type,phone,password, null);
	db.releaseDB(conn);

	if(entity===null || entity.isMember!==1){
		response.errorCode=consts.Error.ERROR_BIND_ERROR;
		response.message=consts.Message.ERROR_BIND_ERROR;
		if(callback){
			callback(response);
		}
	}else{
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(entity);

		if(callback){
			callback(response);
		}
	}
	return true;
	
	
}





async function processDetail(request,response,callback){

	console.log('processDetail');
	
	var data =request.data;
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		
		return;
	}

	var param=null;
	try {
	    param = JSON.parse(data);

	} catch (e) {
	}
	
	var muuid= request.muuid;
	
	var code = null;
	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.authCode!==null && typeof(param.authCode) !=='undefined'){
		code = param.authCode;
	}

	if(param.type !==1 && param.type!==2){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	var type = param.type;

	var isMA = 0;
	
	var userID =0;
	if(param.userID){
		userID= Number(param.userID);
	}
	if(param.isMA && param.isMA ===1){
		isMA = 1;
	}
	
	if(userID >0){
		
		var entity = await member.detail(request.muuid,null,null,userID, null);
		
		if(entity===null){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
		
		}else{
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			response.data = JSON.stringify(entity);
			if(callback){
				callback(response);
			}
		
		}
		return;
	}
	
	var conn = await db.createDB();//(function(conn){
	if(!conn){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	var obj = await member.fetchAppIDandOpenID1(conn,muuid, code, type,null);
	if(obj===null){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		db.releaseDB(conn);
		return;
	
	}
	
	if(!obj.openID){
		db.releaseDB(conn);
		
		var appid = obj.appid;
		var secret = obj.appsecret;
		var err = null;
		if(type===1){
			
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try{
				obj.openID = await oauth2.loginWeixin(appid, secret, code,isMA, null);
			}catch(e){
				err = e;
			}
			
		}else if(type===2){
			
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			try{
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err = e;
			}
		}
		
		
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return false;
		
		}
		conn = await db.createDB();//(function(conn){
		if(!conn){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
			return false;
		}
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type,null);
	
	}
	
	
	
	
	var entity = await member.detail1(conn,request.muuid,obj.openID,type,0,null);
	db.releaseDB(conn);
	if(entity===null){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
	}else{
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		response.data = JSON.stringify(entity);
		if(callback){
			callback(response);
		}
	}
	return true;
		
				
					
			

}



async function processMyCoupons(request, response,callback){
	var data =request.data;
	var phone = null;
	var password = null;
	var muuid = null;
	var code = null;
	var memberName=null;
	var birth = null;
	var gender = null;
	var isMA = 0;
	
	console.log('processMyCoupons::'+ data);
	
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param=null;
	try {
	    param = JSON.parse(data);
	
	} catch (e) {
	}
	
	muuid = request.muuid;
	
	if(param===null || typeof(param.type)==='undefined' || typeof(param.authCode)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'type and authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.type !==1 && param.type!==2){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	if(typeof(param.authCode)==='undefined' || param.authCode===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}
	
	var type = param.type;
	code = param.authCode;

	
	var conn = await db.createDB();
	if(!conn){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	var obj = await member.fetchAppIDandOpenID1(conn,muuid,code,type, null);
	
	if(!obj){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}

		db.releaseDB(conn);

		return;
	}
					
		
	console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));
			
	if(!obj.openID){	
		db.releaseDB(conn);
		var appid = obj.appid;
		var secret = obj.appsecret;
		var err = null;
		if(type ===1){
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try{
				obj.openID = await oauth2.loginWeixin(appid, secret, code,isMA,null);
			}catch(e){
				err = e;
			}
		}else if(type===2){
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			try{
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err =e;
			}
		}
		
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		conn = await db.createDB();
		if(!conn){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
			return false;
		}
		
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type, null);
		
	}
	
	var entity = await member.myCoupons1(conn,muuid,obj.openID, type, null);
	
	db.releaseDB(conn);
	
	if(entity===null){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
	}else{
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(entity);

		if(callback){
			callback(response);
		}
	}
	
	return false;
				
}


 


async function processList(request, response,callback){
	var data =request.data;
	var token = null;
    var keyword = null;
	
	console.log('processList::'+ data);
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':token';
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param=null;
	try {
	    param = JSON.parse(data);
	
	} catch (e) {
	}
	
	var pageNO=0;
	var pageSize=200;
	var totalCount=0;
	token = request.token;

	if(param !== null && param.keyword){
		keyword = param.keyword;
	}
	
	
	if(param !== null && param.pageNO){
		pageNO = param.pageNO;
	}
	
	if(param !== null && param.pageSize){
		pageSize = param.pageSize;
	}
	
	if(param !== null && param.totalCount){
		totalCount = param.totalCount;
	}
	
	var conn = await db.createDB();
	if(!conn){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;
	}
		
	var obj = await member.list1(conn,token,keyword ,pageNO, pageSize, totalCount, null);
	
			
	db.releaseDB(conn);

	if(!obj){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}

		return false;
	}
	
	response.errorCode = consts.Error.NONE_ERROR;
	response.message= consts.Message.OK_MESSAGE;
	response.data = JSON.stringify(obj);

	if(callback){
		callback(response);
	}
	return true;
	
}




async function processPaymentList(request,response,callback){
	
	var data =request.data;
	
	if(!request.token  && ! request.muuid){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	var pageNO=0;
	var pageSize=20;
	var total=0;
	var fromTime =null;// new Date().getTime() -1000*60*60*24*30;
	var toTime = new Date().getTime();
	
	var phone = null;
	var flowType = 1;

	var param = null;

	if(data!==null && typeof(data)!=='undefined'){
	
		try {
		    param = JSON.parse(data);

		} catch (e) {
		
		}

	}
	
	if(param!==null){
		if(param.pageNO!==null && typeof(param.pageNO)!=='undefined'){
			pageNO=param.pageNO;
		}
		
		if(param.pageSize!==null && typeof(param.pageSize)!=='undefined'){
			pageSize=param.pageSize;
		}
		
		if(param.phone!==null && typeof(param.phone)!=='undefined'){
			phone=param.phone;
		}
		
		if(param.total!==null && typeof(param.total)!=='undefined'){
			total=param.total;
		
		}
		if(param.fromTime!==null && typeof(param.from)!=='undefined'){
			fromTime=param.from;
		}
		if(param.toTime!==null && typeof(param.to)!=='undefined'){
			toTime=param.to;
		}
		if(param.flowType){
			flowType=param.flowType;
		}
		
	}
	
	if(pageSize < 0){
		pageSize = 1000000;
	}
	
	var wrap = null;
	var err = null;
	try{
		if(request.token){
			wrap = await member.listPayments(request.token,phone, flowType, pageNO,pageSize,total, fromTime,toTime,null);

		}else{
			wrap = await member.listCustomerPayments(request.muuid,phone, flowType, pageNO,pageSize,total, fromTime,toTime,null);
			
		}
	}catch(e){
		err = e;
	}
	if(err){
		
		response.errorCode=err.number;
		response.message=err.message;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	response.errorCode = consts.Error.NONE_ERROR;
	response.message= consts.Message.OK_MESSAGE;
	response.data = JSON.stringify(wrap);
	if(callback){
		callback(response);
	}
		

	return true;
	
}





async function processUpdate(request, response,callback){
	var data =request.data;
	
	console.log('processAdd::'+ data);
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':token';
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param=null;
	try {
	    param = JSON.parse(data);
	
	} catch (e) {
	}
	
	


	if(param===null || !(param.cardNO)){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':cardNO';
		if(callback){
			callback(response);
		}
		return;
	}
	
	var token = request.token;
	var cardNO = param.cardNO;
	
	
//	if(param===null || typeof(param.type)==='undefined' || typeof(param.authCode)==='undefined'){
//		response.errorCode=consts.Error.ERROR_PARAM;
//		response.message=consts.Message.ERROR_PARAM+'type and authCode';
//		if(callback){
//			callback(response);
//		}
//		return false;
//	}
//	

	
	var conn = await db.createDB();
	if(!conn){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;
	}
		
	var obj = member.update1(conn,token,cardNO ,param, null);
	
			
	db.releaseDB(conn);
	if(!obj){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.ERROR_MEMBER_UPDATE;
		if(callback){
			callback(response);
		}

		return false;
	}
			
	response.errorCode = consts.Error.NONE_ERROR;
	response.message= consts.Message.OK_MESSAGE;

	if(callback){
		callback(response);
	}
	return true;

}





async function processAdd(request, response,callback){
	var data =request.data;
	
	console.log('processAdd::'+ data);
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':token';
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param=null;
	try {
	    param = JSON.parse(data);
	
	} catch (e) {
	}
	
	var memberName=null;
	var phone=null;
	var price = 0.0;
	var token = request.token;

	
	if(param !== null && param.memberName){
		memberName = param.memberName;
	}
	
	if(param !== null && param.price){
		price = param.price;
	}
	
	
	if(param !== null && param.phone){
		phone = param.phone;
	}
	
	
	if(phone===null ){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':phone';
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(memberName === null){
		memberName = phone;
	}

	
	var conn = await db.createDB();
	if(!conn){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	var obj = await member.add1(conn,token,memberName ,phone,price, null);
	
			
	db.releaseDB(conn);
	if(!obj){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}

		return false;
	}
			
	response.errorCode = consts.Error.NONE_ERROR;
	response.message= consts.Message.OK_MESSAGE;

	if(callback){
		callback(response);
	}
	return true;
}


async function processCouponList(request, response,callback){
	var data =request.data;
	var phone = null;
	var password = null;
	var muuid = null;
	var code = null;
	var memberName=null;
	var birth = null;
	var gender = null;
	var isMA = 0;
	
	console.log('processRegister::'+ data);
	
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param=null;
	try {
	    param = JSON.parse(data);
	
	} catch (e) {
	}
	
	muuid = request.muuid;
	
	if(param===null || typeof(param.type)==='undefined' || typeof(param.authCode)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'type and authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.type !==1 && param.type!==2){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	if(typeof(param.authCode)==='undefined' || param.authCode===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}
	
	var type = param.type;
	code = param.authCode;

	
	var conn = await db.createDB();
	
	if(!conn){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;

	}

	var obj = await member.fetchAppIDandOpenID1(conn,muuid,code,type,null);
	
	if(!obj){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}

		db.releaseDB(conn);

		return false;
	}
					
		
	console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));
			
	if(!obj.openID){	
		db.releaseDB(conn);
					
		var appid = obj.appid;
		var secret = obj.appsecret;
		var err = null;
		if(type===1){
			
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try{
				obj.openID= await oauth2.loginWeixin(appid, secret, code,isMA,null);
			}catch(e){
				err =e;
			}
			
		}else if(type===2){
			
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			try{
				obj.openID= await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err =e;
			}
		
		}	
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return false;
						
		}
							
		conn = await db.createDB();
		
		if(!conn){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
			return false;

		}		
		
							
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type, null);
		
	}
				
				// register1(conn,muuid,openid,type,phone,password,name,birth,gender, callback){
	
			
	var entity = await member.listCoupons1(conn,muuid,obj.openID, type, null);					
	db.releaseDB(conn);
	
	if(entity===null){
		response.errorCode=consts.Error.ERROR_MOBILE_USED;
		response.message=consts.Message.ERROR_REGISTER;
		if(callback){
			callback(response);
		}
	}else{
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(entity);

		if(callback){
			callback(response);
		}
	}
	return true;			
	
}




async function processCollectCoupon(request, response,callback){
	var data =request.data;
	var muuid = null;
	var code = null;
	var couponID = null;
	var isMA = 0;
	
	console.log('processRegister::'+ data);
	
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param=null;
	try {
	    param = JSON.parse(data);
	
	} catch (e) {
	}
	
	muuid = request.muuid;
	
	if(param===null || typeof(param.type)==='undefined' || typeof(param.authCode)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'type and authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.type !==1 && param.type!==2){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	if(typeof(param.authCode)==='undefined' || param.authCode===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(typeof(param.couponID)==='undefined' || param.couponID===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':couponID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	couponID = param.couponID;
	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}

	var type = param.type;
	code = param.authCode;
		
	var conn = await db.createDB()
	if(!conn){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;
	}
		
	var obj = await member.fetchAppIDandOpenID1(conn,muuid,code,type, null);
	
	if(!obj){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}

		db.releaseDB(conn);

		return false;
	}
					
		
	console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));
				
	if(!obj.openID){	
		db.releaseDB(conn);
		var appid = obj.appid;
		var secret = obj.appsecret;
		var err = null;
		if(type===1){
			if(isMA===1){
			
				appid = obj.g_appid;
				secret = obj.g_appsecret;
		
			}
			try{
				obj.openID = await oauth2.loginWeixin(appid, secret, code,isMA, null);
			}catch(e){
				err = e;
			}
				
						
		}else if(type===2){
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			try{
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err = e;
			}
		}
		
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		conn = await db.createDB()
		if(!conn){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
			return false;
		}
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type, null);
	
	}		
	
	var entity = await member.collectCoupon1(conn,muuid,obj.openID, type,couponID,null);
	
	db.releaseDB(conn);
	
	if(entity===null){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
	}else{
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(entity);

		if(callback){
			callback(response);
		}
	}
	return true;

	
}



async function processCollectGift(request,response,callback){

	console.log('processCollectGift');
	
	var data =request.data;
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.NEED_MERCHANT;
		response.message=consts.Message.NEED_MERCHANT;
		if(callback){
			callback(response);
		}
		return;
	}

	var muuid = request.muuid;
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}
	
    var param = null;
    

    try {
    	param = JSON.parse(data);
	} catch (e) {
	}
	
    var money = 999999999.00;

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	var phone =null;
	var orderNO = null;
	var ID = null;
	if(param.phone ===null || typeof(param.phone)==='undefined'){

	}else{
		phone=param.phone;
	}
	
	if(param.orderNO ===null || typeof(param.orderNO)==='undefined'){

	}else{
		orderNO=param.orderNO;
	}
	
	if(param.ID ===null || typeof(param.ID)==='undefined'){

	}else{
		ID=param.ID;
	}
	
	
	
	var entity = await member.collectGift(request.muuid,phone,orderNO, ID,null);// function(entity){
    if(entity===null){
		response.errorCode=consts.Error.ERROR_NOT_VIP;
		response.message=consts.Message.ERROR_NOT_VIP;
		if(callback){
			callback(response);
		}
		return false;
    	
    }
	
	response.errorCode = consts.Error.NONE_ERROR;
	response.message= consts.Message.OK_MESSAGE;
	response.data=JSON.stringify(entity);
	if(callback){
		callback(response);
	}
		
	return false;
	
}



async function processVIPCheck(request,response,callback){

	console.log('processVIPCheck');
	
	var data =request.data;
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.NEED_MERCHANT;
		response.message=consts.Message.NEED_MERCHANT;
		if(callback){
			callback(response);
		}
		return;
	}

	var muuid = request.muuid;
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}
	
    var param = null;
    

    try {
    	param = JSON.parse(data);
	} catch (e) {
	}
	
    var money = 999999999.00;

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	var checkNO =null;
	var orderNO = null;
	if(param.checkNO ===null || typeof(param.checkNO)==='undefined'){

	}else{
		checkNO=param.checkNO;
	}
	
	if(param.orderNO ===null || typeof(param.orderNO)==='undefined'){

	}else{
		orderNO=param.orderNO;
	}
	
	var type =1;
	
	if(param.type ===null || typeof(param.type)==='undefined'){

	}else{
		type=param.type;
	}
	
	if(param.money ===null || typeof(param.money)==='undefined'){
	}else{
	    money = param.money;
	}
	
	var code = null;
	if(param.authCode ===null || typeof(param.authCode)==='undefined'){

	}else{
		code=param.authCode;
	}
	var isMA = 0;
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}
	
	if(code ===null && checkNO===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':checkNO or authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(checkNO !== null){
		var entity = await member.checkVIP(request.muuid,checkNO,orderNO, money,null);// function(entity){
        if(entity===null){
    		response.errorCode=consts.Error.ERROR_NOT_VIP;
    		response.message=consts.Message.ERROR_NOT_VIP;
    		if(callback){
    			callback(response);
    		}
    		return false;
        	
        }
		
    	response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data=JSON.stringify(entity);
   		if(callback){
   			callback(response);
   		}

   		return true;
		
	}else{
		
		var conn = await db.createDB();
		if(!conn){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
			return false;
		}
			
		var obj = await member.fetchAppIDandOpenID1(conn,muuid,code,type, null);
		
		if(obj===null){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}

			db.releaseDB(conn);

			return;
		}
				
		console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));
				
		if(!obj.openID){	
			db.releaseDB(conn);
			var appid = obj.appid;
			var secret = obj.appsecret;
			var err = null;
						
			if(type ===1){
				if(isMA===1){
					appid = obj.g_appid;
					secret = obj.g_appsecret;
				}
				try{
					obj.openID = await oauth2.loginWeixin(appid, secret, code,isMA,null);
				}catch(e){
					err = e;
				}
						
			}else if(type===2){
				appid = obj.alappid;
				secret = null;//obj.g_appsecret;
				try{
					obj.openID = await oauth2.loginZfb(appid, secret, code,null);
				}catch(e){
					err = e;
				}
			
			}
			
			if(err){
				response.errorCode=err.number;
				response.message=err.message;
				if(callback){
					callback(response);
				}
				return true;
				
			}
								
					
			conn = await db.createDB();//){
			if(!conn){
				response.errorCode=consts.Error.ERROR_GENERAL;
				response.message=consts.Message.UNKNOWN;
				if(callback){
					callback(response);
				}
				return false;
			}
									
			var oo = await member.saveCodeTemp1(conn,code,obj.openID,type, null);
		}
		
		var entity = await member.myOptimalList1(conn,muuid,obj.openID, type,money, null);
		db.releaseDB(conn);

		if(entity===null){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
		}else{
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(entity);

			if(callback){
				callback(response);
			}
		}
		return true;
	
	}
	return false;
	
}




module.exports = {
		processBind:processBind,
		processOfficialLogin:processOfficialLogin,
		processDetail:processDetail,
		processRegister:processRegister,
		processCouponList:processCouponList,
		processList:processList,
		processCollectCoupon:processCollectCoupon,
		processMyCoupons:processMyCoupons,
		processVIPCheck:processVIPCheck,
        processPaymentList:processPaymentList,
		processAdd:processAdd,
		processUpdate:processUpdate,
		processCollectGift:processCollectGift,

	  };
